<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >

                    <!--                    <FormItem :label="t('postage.type')" type="radio" v-model="baTable.form.items!.type" prop="type" :data="{ content: { '1': t('postage.type 1'), '2': t('postage.type 2') } }" :placeholder="t('Please select field', { field: t('postage.type') })" />-->
<!--                    <FormItem :label="t('postage.sweight')" min="0" type="number" prop="sweight" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.sweight" :placeholder="t('Please input field', { field: t('postage.sweight') })" />-->
                    <FormItem :label="t('postage.sprice')" min="0" type="number" prop="sprice" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.sprice" :placeholder="t('Please input field', { field: t('postage.sprice') })" />
<!--                    <FormItem :label="t('postage.xweight')" min="0" type="number" prop="xweight" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.xweight" :placeholder="t('Please input field', { field: t('postage.xweight') })" />-->
<!--                    <FormItem :label="t('postage.xprice')" min="0" type="number" prop="xprice" :input-attr="{ step: 1 }" v-model.number="baTable.form.items!.xprice" :placeholder="t('Please input field', { field: t('postage.xprice') })" />-->

                    <div style="display: flex">
                        <div class="el-form-item__label" style="width: 160px;">
                            <span style="color: #f56c6c;margin-right: 4px">*</span>城市
                        </div>
                        <el-tree
                            :data="baTable.form.extend.city_list"
                            show-checkbox
                            node-key="id"
                            :default-checked-keys=baTable.form.items!.citys_ids
                            :props="defaultProps"
                            @check="current_change"
                            @node-collapse="node_collapse"
                            class="w100"
                        />

                    </div>


                    <FormItem :label="t('postage.citys_ids')" type="remoteSelects" v-model="baTable.form.items!.citys_ids" prop="citys_ids" :input-attr="{ pk: 'xxz_.id', field: 'name', 'remote-url': '' }" style="display: none" :placeholder="t('Please select field', { field: t('postage.citys_ids') })" />
                    <!--                    <FormItem :label="t('postage.citys')" type="textarea" v-model="baTable.form.items!.citys" prop="citys" :input-attr="{ rows: 3 }" @keyup.enter.stop="" @keyup.ctrl.enter="baTable.onSubmit(formRef)" :placeholder="t('Please input field', { field: t('postage.citys') })" />-->
                    <!--                    <FormItem :label="t('postage.city_json')" type="string" v-model="baTable.form.items!.city_json" prop="city_json" :placeholder="t('Please input field', { field: t('postage.city_json') })" />-->
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type {ElTree, FormInstance, FormItemRule} from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import createAxios from "/@/utils/axios";
import {ElMessage} from "element-plus";
import type Node from "element-plus/es/components/tree/src/model/node";

const current_change = (data, checkedData) => {
    baTable.form.items.citys_ids = checkedData.checkedKeys
}
const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    sweight: [buildValidatorData({ name: 'required', title: t('postage.sweight') })],
    sprice: [buildValidatorData({ name: 'required', title: t('postage.sprice') })],
    xweight: [buildValidatorData({ name: 'required', title: t('postage.xweight') })],
    xprice: [buildValidatorData({ name: 'required', title: t('postage.xprice') })],
    create_time: [buildValidatorData({ name: 'date', title: t('postage.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('postage.update_time') })],
})



const treeNodeClass = (data: anyObj, node: Node) => {
    if (node.isLeaf) return ''
    let addClass = true
    for (const key in node.childNodes) {
        if (!node.childNodes[key].isLeaf) {
            addClass = false
        }
    }
    return addClass ? 'penultimate-node' : ''
}


const defaultProps = {
    children: 'children',
    label: 'label',
    class: treeNodeClass
}


const node_collapse = () => {


}


</script>

<style lang="scss" scoped>


:deep(.penultimate-node) {
    .el-tree-node__children {
        padding-left: 40px;
        white-space: pre-wrap;
        line-height: 12px;
        .el-tree-node {
            display: inline-block;
        }
        .el-tree-node__content {
            padding-left: 5px !important;
            padding-right: 5px;
            .el-tree-node__expand-icon {
                display: none;
            }
        }
    }
}

//::v-deep(.el-tree-node.is-expanded>.el-tree-node__children){
//    display: flex;
//    flex-wrap: wrap;
//}
</style>
